﻿using System;
using PPI.Entities.Distribution;
using PPI.Data;
using System.Data;
using System.Data.SqlClient;

namespace PPIDataAccess.Distribution
{
    public class DInspectionResult
    {
        public static DataSet Select(string connectionKey)
        {
            SqlDataAdapter da = null;

            try
            {
                da = new SqlDataAdapter("up_tb_Vendor_Distribution_InspectionResults_SelectAll", DBHelper.GetConnection(connectionKey));
                da.SelectCommand.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                da.Fill(ds);

                return ds;
            }
            catch (Exception exc)
            {
                throw new Exception("Error executing up_tb_Vendor_Distribution_InspectionResults_SelectAll", exc);
            }
            finally
            {
                if (da != null)
                    da.SelectCommand.Connection.Close();
            }
        }

        public static int Insert(EDistributionInspectionResult inspectionResult, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_InspectionResults_Insert", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region Pole

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                #endregion

                #region Type

                if (inspectionResult.InspectionType.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TypeFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TypeFk", ParameterDirection.Input, true, inspectionResult.InspectionType.Id));

                #endregion

                #region ResultType

                if (inspectionResult.Resultype.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, inspectionResult.Resultype.Id));

                #endregion

                #region Comments

                if (String.IsNullOrEmpty(inspectionResult.Comments))
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, inspectionResult.Comments));

                #endregion

                #region Electrical

                cmd.Parameters.Add(DBHelper.CreateBitParameter("@bElectrical", ParameterDirection.Input, true, inspectionResult.IsElectrical));

                #endregion

                cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_DistributionInspectionResult_Id", ParameterDirection.ReturnValue, false, 0));

                cmd.ExecuteNonQuery();
                tr.Commit();

                return (Int32)cmd.Parameters["@Vendor_DistributionInspectionResult_Id"].Value;
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();

                return 0;
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }


        public static void Update(EDistributionInspectionResult inspectionResult, Int32 poleId, string connectionKey)
        {
            SqlCommand cmd = null;
            SqlTransaction tr = null;

            try
            {
                cmd = new SqlCommand("up_tb_Vendor_Distribution_InspectionResults_Update", DBHelper.GetConnection(connectionKey));
                tr = cmd.Connection.BeginTransaction();
                cmd.Transaction = tr;

                cmd.CommandType = CommandType.StoredProcedure;

                #region - Inspection Result Id -

                if (inspectionResult.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Results_ResultId", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_Results_ResultId", ParameterDirection.Input, true, inspectionResult.Id));

                #endregion

                #region Pole

                if (poleId == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_PoleFk", ParameterDirection.Input, true, poleId));

                #endregion

                #region Type

                if (inspectionResult.InspectionType.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TypeFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_TypeFk", ParameterDirection.Input, true, inspectionResult.InspectionType.Id));

                #endregion

                #region ResultType

                if (inspectionResult.Resultype.Id == Int32.MinValue)
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateIntParameter("@Vendor_Distribution_ResultTypeFk", ParameterDirection.Input, true, inspectionResult.Resultype.Id));

                #endregion

                #region Comments

                if (String.IsNullOrEmpty(inspectionResult.Comments))
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, DBNull.Value));
                else
                    cmd.Parameters.Add(DBHelper.CreateTextParameter("@Comments", ParameterDirection.Input, true, inspectionResult.Comments));

                #endregion

                #region Electrical

                cmd.Parameters.Add(DBHelper.CreateBitParameter("@bElectrical", ParameterDirection.Input, true, inspectionResult.IsElectrical));

                #endregion

                cmd.ExecuteNonQuery();
                tr.Commit();
            }
            catch (Exception ex)
            {
                if (tr != null)
                    tr.Rollback();
            }
            finally
            {
                if (cmd != null) cmd.Connection.Close();
            }
        }
    }
}
